package com.zqx.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zqx.domain.Dorm;
import com.zqx.domain.Student;
import com.zqx.service.DormService;
import com.zqx.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("student")
public class StudentController {
    @Autowired
    private StudentService studentService;
    @Autowired
    private DormService dormService;
    @Value("${location}")
    private String location;
    //查询所有学生
    @RequestMapping("listStudent")
    public String listStudent(@RequestParam(value = "pageNum", defaultValue = "1",required = false)Integer pageNum,
                              @RequestParam(value = "pageSize", defaultValue = "4",required = false)Integer pageSize,
                              String sname, Model model) {
        if (pageNum < 0 || pageNum.equals("") || pageNum == null) {
            pageNum = 1;
        }
        if (pageNum < 0 || pageNum.equals("") || pageNum == null) {
            pageSize = 4;
        }
        PageHelper.startPage(pageNum, pageSize);
        //条件构造器：MP用来查询一些特殊的字段
        //模糊查询
        QueryWrapper<Student> qw = new QueryWrapper<>();
        if (sname != null) {
            qw.like("sname", sname);
        }
        List<Student> list = studentService.list(qw);
        for (Student student1 : list) {
            if (student1.getDid() == null) {
                student1.setDorm("暂未分配宿舍");
            } else {
                Integer did = student1.getDid();
                Dorm dorm = dormService.getById(did);
                student1.setDorm(dorm.getBuild() + "-" + dorm.getDormid());
            }
        }
            PageInfo<Student> pageInfo = new PageInfo<>(list);
            model.addAttribute("pageInfo", pageInfo);
            return "admin-student-list";
        }
        //添加学生
        @RequestMapping("saveStudent")
        public String saveStudent(Student student, MultipartFile file){
            if (!file.isEmpty()){
                transfile(student, file);
            }
            boolean save = studentService.save(student);
            //重定向再过一边listStudent方法
            return "redirect:/student/listStudent";
        }
    //上传图片的方法
    private void transfile(Student student, MultipartFile file) {
        String originalFilename = file.getOriginalFilename();
        int index = originalFilename.lastIndexOf(".");
        String suffix = originalFilename.substring(index);
        String prefix = System.nanoTime()+"";
        String path = prefix +suffix;
        File file1 = new File(location);
        if (!file1.exists()){
            file1.mkdirs();
        }
        File file2 = new File(file1, path);
        try {
            file.transferTo(file2);
        } catch (IOException e) {
            e.printStackTrace();
        }
        student.setSimage(path);
    }
    //修改数据回显
    @RequestMapping("preUpdateStudent/{id}")
    public String preUpdateStudent(@PathVariable Integer id, Model model){
        Student byId = studentService.getById(id);
        model.addAttribute("student", byId);
        return "admin-student-update";
    }
    //修改学生信息
    @RequestMapping("updateStudent")
    public String updateStudent(Student student, MultipartFile file){
        if (!file.isEmpty()){
            transfile(student,file);
        }
        studentService.updateById(student);
        return "redirect:/student/listStudent";
    }
    //删除学生信息
    @RequestMapping("delStudent/{id}")
    public String delStudent(@PathVariable Integer id){
        studentService.removeById(id);
        return "redirect:/student/listStudent";
    }
    //批量删除学生信息
    @ResponseBody
    @RequestMapping("batchDeleteStudent")
    public String batchDeleteStudent(String idList){
        String[] split = StrUtil.split(idList, ",");
        List<Integer> list = new ArrayList<>();
        for (String s:split
             ) {
            if (!s.isEmpty()){
                int i = Integer.parseInt(s);
                list.add(i);
            }
        }
        boolean b = studentService.removeByIds(list);
        if (b){
            return "OK";
        }else {
            return "error";
        }
    }
}
